package com.vieewer.btdemo.view.wifi;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * A simple server socket that accepts connection and writes some data on
 * the stream.
 */
public class DataServerAsyncTask extends
        AsyncTask<Void, Void, String> {

    private Context context;



    /**
     * @param
     */
    public DataServerAsyncTask(Context ctx) {
        this.context = ctx;
    }

    @Override
    protected String doInBackground(Void... params) {
        InputStream inputstream = null;
        ByteArrayOutputStream baos = null;
        try {
            Log.i("tag", "data doinback");
            ServerSocket serverSocket = new ServerSocket(8887);
            //设置端口复用
            serverSocket.setReuseAddress(true);
            Log.i("tag","串口创建完成");
            Socket client = serverSocket.accept();


            TransferPhotosManager.peerAddress = client.getInetAddress().getHostAddress();//获取peer对面的ip
            TransferPhotosManager.clientIp.add(TransferPhotosManager.peerAddress);

            for(int i = 0; i< TransferPhotosManager.clientIp.size(); i++){
                Log.i("tag","DataTask clientIp:"+ TransferPhotosManager.clientIp.get(i)+"\n");
            }
            Log.i("tag", "DataTask PeerIpIP is :" + TransferPhotosManager.peerAddress);

            Log.i("tag","阻塞已取消");
//            InputStream inputstream = client.getInputStream();
//            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            inputstream = client.getInputStream();
            baos = new ByteArrayOutputStream();
            int i;
            while ((i = inputstream.read()) != -1) {
                baos.write(i);
            }

//            String str = baos.toString();
//            serverSocket.close();
//            return str;
//
//        } catch (IOException e) {
//            Log.e("tag", e.toString());
//            return null;
//        }
            String str = baos.toString();
            client.close();
            serverSocket.close();
            return str;

        } catch (IOException e) {
            Log.e("tag", e.toString());
            return null;
        }finally {
            try{
                if(inputstream != null){
                    inputstream.close();
                }
               if(baos !=null){
                   baos.close();
               }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(String result) {

        Log.i("tag","RecvData；"+result);
        Log.i("tag", "data onpost");
        if(result != null ){
            //清空设备列表信息
            TransferPhotosManager.peersshow.clear();

            TransferPhotosManager.clientName.add(result);
            for(int i = 0; i< TransferPhotosManager.clientName.size(); i++){
                Log.i("tag","DataTask clientName:"+ TransferPhotosManager.clientName.get(i)+"\n");
            }
            Toast.makeText(context, "WiFi Connection success", Toast.LENGTH_SHORT).show();
//            Toast.makeText(activity, result, Toast.LENGTH_SHORT).show();
            Log.i("tag","接受到的Data："+result);
        }else{
//            Toast.makeText(activity, "网络连接已断开()！", Toast.LENGTH_SHORT).show();
        }



//        if (result != null) {
//        //说明为GroupOwner
//            statusText.setText("RecvData is :" + result);
//        }
    }

    /*
     * (non-Javadoc)
     *
     * @see android.os.AsyncTask#onPreExecute()
     */
    @Override
    protected void onPreExecute() {

    }

}